<?
#include("GUI/HtmlLink.class.php");
#include "DB/DataBase.class.php";
#include "GridSource.class.php";
#include "Grid.class.php";

class GridSortPage{
	


	public static function SortQuery($sql, $sort_by){

		$has_order_clause = strrpos($sql, "ORDER BY");
		if(!$has_order_clause && $sort_by != "")
			$sql.= " ORDER BY ".$sort_by;
		elseif($has_order_clause && $sort_by != ""){
			$stop =		strpos($sql, "ORDER BY");
			$new = substr($sql, 0, $stop);
			$sql = $new.= " ORDER BY ".$sort_by;
		}
							
		return $sql;
	}
	
	static function GetHeader($sql){
		$columnNames = DataBase::FieldsName($sql);
		$html = "<thead>\n<tr>\n";
		foreach($columnNames as $key => $value)
			$html .= "<td><a href=\"".$_SERVER['PHP_SELF']."?order=$value\">".$value."</a></td>";
		
		$html .= "<td>Opciones</td>";
		$html .= "</tr>\n</thead>\n";
		return $html;
	}

	public static function Render($qry, $list, $orderby, $tampag = 50, $pag){

	$original_qry = $qry;
	$qry = GridSortPage::SortQuery($qry, $orderby);
	$qry =  GridSortPage::PaginateQuery($qry, $pag, $tampag);

	
	$matrix = DataBase::ExecuteQuery($qry);

		$html = "\n<table>\n";
		$html .= GridSortPage::GetHeader($qry);
					 
		foreach($matrix as $key => $rows){	
			$html .= "<tr>";
							
			foreach($rows as $index => $column){					
					if($key%2 == 0)		$html .= "<td>".$column."</td>";
					else				$html .= "<td class=\"selectedRow\">".$column."</td>";
			}		

			#============================================================================
			
			#============================================================================
			
			#-------
			$html .= "<td>";
			if(isset($has_header) && $key == 0){
				$html .= "Opciones";
			}else{
			
			/*$html .= "<ul id=\"sddm\">
					    <li>
						<a href=\"#\" onmouseover=\"mopen('m$key')\" onmouseout=\"mclosetime()\">Opciones</a> 
						<div id=\"m$key\" onmouseover=\"mcancelclosetime()\" onmouseout=\"mclosetime()\">";*/
			
				foreach($list as $url)
				{
					if(get_class($url)=='HTMLCheckBox')
						$html.=$url->fillfromColumn($rows,"msgs[]",0)."\n";
					else 
						$html .= $url->Render($rows)."\n";
				}
					
			$html .= "</div>
					</li>
				</ul>
				<div style=\"clear:both\"></div>";
			}
			$html .= "</td>";
			#-------				
			$html .= "\n</tr>\n";	
			if(isset($has_header) && $key ==0) $html .= "</thead>";
	
		}
		$html .= "</table>\n";	
		$html .= GridSortPage::GetNavigationControlsHTML($original_qry, $tampag, $orderby);
		return $html;
	 } #GetHTMLTable
	 
	 
	 #------------------------------PAGINATOR--------------------------------------
	 	public static function PaginateQuery($sql, $pag = 1, $tam_pag = 50){
	
	
		if(!$pag) $pag=1;
	
		$res = DataBase::ExecuteAndReturnResult($sql);
		$nrows = mysql_num_rows($res);
		$npaginas = ceil($nrows / $tam_pag);

		$liminf = ($pag -1) * $tam_pag;
		$limsup = $liminf + $tam_pag;
		
		return $sql .= " LIMIT $liminf, $tam_pag";
	}
	
	public static function GetNavigationControlsHTML($sql, $tam_pag, $orderby){
		
		$ajax=1;
		
		$res = DataBase::ExecuteAndReturnResult($sql);
		$nrows = mysql_num_rows($res);
		$npaginas = ceil($nrows / $tam_pag);
		
	

			$html = "<div align='center'>";
			for ($i = 1; $i <= $npaginas; $i++) 
			{
				if ($ajax==1)
					$html .= "<a href='javascript:;' onClick='get(\"" . $_SERVER['PHP_SELF'] . "?pag=$i&order=$orderby\",\"content\")' >$i</a> -";
				else
					$html .= "<a href='" . $_SERVER['PHP_SELF'] . "?pag=$i&order=$orderby'>$i</a> -";
			}
			$html = substr($html,0,strlen($html)-1); #stripping the last dash
			return $html .= "</div>";
	}

	 #----------------------------------------------------------------------------
	 
	 
}

#$a = new htmlLink("#","Perfil",0,"?id=","newWindow('../ver_perfil.php?id=*','windowTittle')");
#$b = new htmlLink("../update/socios.php?id=","Editar",0,"", "");
#$c = new htmlLink("../delete/socios.php?tabla=socios&redirect=../select/socios.php&id=", "Eliminar", 0, "", "");
#$links = array($a, $b, $c);

#$sql = "SELECT id, nombre, apellido_paterno FROM socios";
#echo GridSortPage::Render("SELECT id, nombre, apellido_paterno FROM socios", $links, $_REQUEST['order'], 10, $_REQUEST['pag']);
?>